<?php
/**
 * GiftController
 *
 * PHP Version 5.4
 *
 * @author liuxu <liuxuphp@icesmart.cn>
 */

namespace Api\Controller;

use Model\FordProportionModel;
use Model\GiftCostLogsModel;
use Model\GiftModel;
use Model\UserModel;
use Think\Controller;
use Model\RoomModel;

class GiftController extends BaseController
{
    /**
     * 礼物列表
     * @http-method GET
     */
    public function find()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $giftModel = new GiftModel();
        $giftList = $giftModel->findByFilter(['status' => 1]);

        $result = [];
        if($giftList){
            $result['code'] = 200;
            $result['msg'] = $giftList;
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据不存在';
        }

        $this->ajaxReturn($result, 'JSON');
    }

    /**
     * 礼物消费
     * @http-method POST
     */
    public function cost()
    {
        $url = '/Api/'.CONTROLLER_NAME.'/'.ACTION_NAME;
        $validate = $this->validateInterface($url);
        if(!$validate){
            $result['code'] = 404;
            $result['msg'] = '此接口已被禁用';

            $this->ajaxReturn($result, 'JSON');
        }
        $userId = I('post.userId');
        $roomId = I('post.roomId');
        $giftId = I('post.giftId');
        $createTime = date('Y-m-d H:i:s');
        $result = [];

        if(empty($userId) || empty($roomId) || empty($giftId)){
            $result['code'] = 404;
            $result['msg'] = '参数异常';

            $this->ajaxReturn($result, 'JSON');
        }

        $giftModel = new GiftModel();
        $giftInfo = $giftModel->get($giftId);

        $userModel = new UserModel();
        $userInfo = $userModel->get($userId);
        if(!$userInfo){
            $result['code'] = 404;
            $result['msg'] = '消费者信息未找到';

            $this->ajaxReturn($result, 'JSON');
        }

        $costResult = ($userInfo['balanceMoney'] - $giftInfo['price']);

        if($costResult < 0){
            $result['code'] = 404;
            $result['msg'] = '消费者余额不足';

            $this->ajaxReturn($result, 'JSON');
        }

        $userModel->where('id = '.$userId)->setField('balanceMoney', $costResult);

        $roomModel = new RoomModel();
        $roomInfo = $roomModel->get($roomId);
        if(!$roomInfo){
            $result['code'] = 404;
            $result['msg'] = '直播间未找到';

            $this->ajaxReturn($result, 'JSON');
        }

        $giftCostModel = new GiftCostLogsModel();
        $giftCostResult = $giftCostModel->add([
            'roomId' => $roomId,
            'roomName' => $roomInfo['name'],
            'userId' => $userId,
            'giftId' => $giftId,
            'price' => $giftInfo['price'],
            'createTime' => $createTime
        ]);

        if($giftCostResult){
            $proportionModel = new FordProportionModel();
            $proportionInfo = $proportionModel->order('createTime DESC')->limit(1)->find();
            $ford = round($giftInfo['price'] / $proportionInfo['unitSystem'], 2);
            //增加主播福特值总额
            $userModel->where('id = '.$userId)->setInc('fordTotal', $ford);
            //增加可兑换福特值
            $userModel->where('id = '.$userId)->setInc('fordAvailable', $ford);

            $result['code'] = 200;
            $result['msg'] = 'ok';
            $result['balance'] = $costResult;
        }else{
            $result['code'] = 404;
            $result['msg'] = '数据添加失败';
        }
        $this->ajaxReturn($result, 'JSON');
    }
} 